package cn.xspace.app.filemg.view;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.BaseRProxy;

import java.util.ArrayList;

import cn.xspace.app.filemg.bean.FileInfo;
import cn.xspace.app.filemg.util.FileMg;

public class OtherAdapter extends ArrayAdapter<FileInfo> {
	private Context context;

	public OtherAdapter(Context context, ArrayList<FileInfo> objects) {
		super(context, 0, objects);
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(BaseRProxy.file_app_child_item, null);
			viewHolder = new ViewHolder(convertView);
		
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		FileInfo info=getItem(position);
		viewHolder.title.setText(info.getName());
		viewHolder.appSize.setText(FileMg.FormetFileSize(info.getSize()));
		viewHolder.appTime.setText(info.getTime());
		if ("pdf".equals(info.getFileType())) {
			viewHolder.image
					.setBackgroundResource(BaseRProxy.file_icon_pdf);
		} else if ("jpg".equals(info.getFileType())
				|| "png".equals(info.getFileType())) {
			viewHolder.image.setBackgroundDrawable(info.getApkIoc());
		} else if ("jar".equals(info.getFileType())
				|| "rar".equals(info.getFileType())) {
			viewHolder.image
					.setBackgroundResource(BaseRProxy.file_icon_rar);
		} else if ("zip".equals(info.getFileType())) {
			viewHolder.image
					.setBackgroundResource(BaseRProxy.file_icon_zip);
		} else if ("tex".equals(info.getFileType())
				|| "ebk3".equals(info.getFileType())) {
			viewHolder.image
					.setBackgroundResource(BaseRProxy.file_icon_txt);
		} else {
			viewHolder.image
					.setBackgroundResource(BaseRProxy.file_icon_default);
		}
		viewHolder.mCheckBox.setChecked(info.isCheckFlag());
		return convertView;
	}

	class ViewHolder {
		public TextView title;
		public ImageView image;
		public CheckBox mCheckBox;
		public TextView appSize;
		public TextView appTime;
		public ViewHolder(View v){
			title = (TextView) v.findViewById(BaseRProxy.ItemText);
			mCheckBox = (CheckBox) v.findViewById(BaseRProxy.box);
			image = (ImageView) v.findViewById(BaseRProxy.ItemImage);
			appSize = (TextView) v.findViewById(BaseRProxy.ItemText_size);
			appTime = (TextView) v.findViewById(BaseRProxy.ItemText_time);
		}
	}

}
